package com.rainng.coursesystem.dao;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.controller.admin.vo.SysUserQueryReqDTO;
import com.rainng.coursesystem.controller.admin.vo.SysUserResDTO;
import com.rainng.coursesystem.dao.mapper.RoleMapper;
import com.rainng.coursesystem.model.entity.RoleEntity;
import com.rainng.coursesystem.model.entity.UserGroupEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@Slf4j
public class RoleDAO extends BaseDAO {
    private final RoleMapper mapper;

    public RoleDAO(RoleMapper mapper) {
        this.mapper = mapper;
    }

    public RoleEntity getByUsername(String username) {
        LambdaQueryWrapper<RoleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleEntity::getUserName, username);
        return mapper.selectOne(wrapper);
    }

    public int insert(RoleEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(String id) {
        return mapper.deleteById(id);
    }

    public RoleEntity get(String id) {
        return mapper.selectById(id);
    }

    public List<Map<String, String>> getUserByRoleId(String groupId) {
        return mapper.queryRoleNameByGroupId(groupId);
    }


    public int update(RoleEntity entity) {
        return mapper.updateById(entity);
    }


    public List<RoleEntity> list() {
        return mapper.selectList(new LambdaQueryWrapper<>());
    }

    public long count(QueryWrapper<RoleEntity> q) {
        return mapper.selectCount(q);
    }

    public List<RoleEntity> getAllList(QueryWrapper<RoleEntity> q) {
        return mapper.selectList(q);
    }

    public List<SysUserResDTO> showlist(SysUserQueryReqDTO vo) {

        Page<RoleEntity> pageParam = new Page<>(vo.getPageNum(), vo.getPageSize());
        IPage<RoleEntity> pageModel = mapper.queryRolePageList(vo, pageParam);
        List<RoleEntity> pos = pageModel.getRecords();
        List<SysUserResDTO> res = new ArrayList<>();
        if (pos.size() > 0) {
            for (RoleEntity r : pos) {
                SysUserResDTO dto = new SysUserResDTO();
                BeanUtil.copyProperties(r, dto);
                if ("0".equals(r.getRole()))
                    dto.setRoleName("公司");
                if ("1".equals(r.getRole()))
                    dto.setRoleName("主检");
                if ("2".equals(r.getRole()))
                    dto.setRoleName("组长");
                if ("3".equals(r.getRole()))
                    dto.setRoleName("超级管理员");
                if ("4".equals(r.getRole()))
                    dto.setRoleName("领导");
                if ("5".equals(r.getRole()))
                    dto.setRoleName("设备管理员");
                if ("6".equals(r.getRole()))
                    dto.setRoleName("场地管理员");
                if ("7".equals(r.getRole()))
                    dto.setRoleName("企业");
                if ("8".equals(r.getRole()))
                    dto.setRoleName("试验员");
                res.add(dto);
            }
        }
        return res;
    }


    public Integer getRoleTotal(SysUserQueryReqDTO vo) {
        Integer total = mapper.queryRoleCount(vo);
        return total;
    }

    public List<String> getUserIdByUserName(String vo) {
        List<String> name = mapper.getUserIdByUserName(vo);
        return name;
    }


}
